package com.erp.produce.controller;

import com.erp.common.ResponseMessage;
import com.erp.produce.entity.Goods;
import com.erp.produce.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("产品信息接口")
@RestController
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    @ApiOperation("返回所有产品信息")
    @GetMapping("/list")
    public ResponseMessage list(){
        List<Goods> list = goodsService.list();
        if (list.size() > 0) {
            return new ResponseMessage(list);
        } else {
            return ResponseMessage.fail("无相关商品信息");
        }
    }

    @ApiOperation("增加产品信息")
    @PostMapping("/add")
    public ResponseMessage addGoods(Goods goods){
        if (goods.getGoodName().isEmpty()) {
            return ResponseMessage.fail("商品名不能为空");
        }
        if (goodsService.add(goods)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("增加商品失败");
    }


    @ApiOperation("更新客户信息")
    @PostMapping("/update")
    public ResponseMessage updateGoods(Goods goods){
        if (goodsService.update(goods)) {
            return ResponseMessage.ok();
        }
        return ResponseMessage.fail("更新商品信息失败");
    }

    @ApiOperation("分页展示产品信息")
    @GetMapping("/list/{page}/{size}")
    public ResponseMessage<List<Goods>> listPage(@PathVariable("page") int page, @PathVariable("size") int size){
        List<Goods> goods = goodsService.listPage(page, size);
        if (goods.size() == 0) {
            return ResponseMessage.fail("无相关商品信息");
        }
        return new ResponseMessage<>(goods);
    }

    @ApiOperation("分页展示产品信息（带模糊查询）")
    @GetMapping("/list/name/{page}/{size}")
    public ResponseMessage<List<Goods>> listPageLikeName(@PathVariable("page") int page,@PathVariable("size") int size,String name){
        List<Goods> goods = goodsService.listPageLike(page, size, name);
        if (goods.size() == 0) {
            return ResponseMessage.fail("无相关商品信息");
        }
        return new ResponseMessage<>(goods);
    }


    @ApiOperation("删除产品信息（支持多选）")
    @PostMapping("/list/delete")
    public ResponseMessage deleteGoodsList(@RequestParam(value = "idArray[]") Long[] ids) {
        if (goodsService.deleteIds(ids)) {
            return ResponseMessage.ok();
        } else{
            return ResponseMessage.fail("删除失败，请重试");
        }
    }
}
